package com.harmony.pilot.llm.controller;

import com.harmony.pilot.common.dto.LLMRequest;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.llm.service.LLMService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * LLM服务控制器
 */
@Slf4j
@RestController
@RequestMapping("/llm")
@RequiredArgsConstructor
public class LLMController {
    
    private final LLMService llmService;
    
    /**
     * 生成代码
     */
    @PostMapping("/generate")
    public RestResult<Map<String, Object>> generate(@Valid @RequestBody LLMRequest request) {
        log.info("Received LLM generation request");
        
        try {
            Map<String, Object> result = llmService.generate(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to generate with LLM", e);
            return RestResult.error("Failed to generate: " + e.getMessage());
        }
    }
    
    /**
     * 代码补全
     */
    @PostMapping("/complete")
    public RestResult<Map<String, Object>> complete(@Valid @RequestBody LLMRequest request) {
        log.info("Received LLM completion request");
        
        try {
            Map<String, Object> result = llmService.complete(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to complete with LLM", e);
            return RestResult.error("Failed to complete: " + e.getMessage());
        }
    }
    
    /**
     * 代码分析
     */
    @PostMapping("/analyze")
    public RestResult<Map<String, Object>> analyze(@Valid @RequestBody LLMRequest request) {
        log.info("Received LLM analysis request");
        
        try {
            Map<String, Object> result = llmService.analyze(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to analyze with LLM", e);
            return RestResult.error("Failed to analyze: " + e.getMessage());
        }
    }
    
    /**
     * 聊天对话
     */
    @PostMapping("/chat")
    public RestResult<Map<String, Object>> chat(@Valid @RequestBody LLMRequest request) {
        log.info("Received LLM chat request");
        
        try {
            Map<String, Object> result = llmService.chat(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to chat with LLM", e);
            return RestResult.error("Failed to chat: " + e.getMessage());
        }
    }
    
    /**
     * 切换LLM提供商
     */
    @PostMapping("/switch-provider")
    public RestResult<String> switchProvider(@RequestParam String provider) {
        log.info("Switching LLM provider to: {}", provider);
        
        try {
            llmService.switchProvider(provider);
            return RestResult.success("Switched to provider: " + provider);
        } catch (Exception e) {
            log.error("Failed to switch provider", e);
            return RestResult.error("Failed to switch provider: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public RestResult<String> health() {
        return RestResult.success("LLM Service is running");
    }
}

